#!/usr/local/bin/python
# __    _____ _____ _____    ___     ___ 
#|  |  |  _  |     |   __|  |_  |   |   |
#|  |__|     | | | |__   |   _| |_ _| | |
#|_____|__|__|_|_|_|_____|  |_____|_|___|

#
# Author: Carl Javier 4088697
# Date: 22 September 2008

# Used to initiate all ami and cdr scripts
# then used to inject values into the 
# ganglia monitoring system.


# This script will be run via a cron job
# and must be as root

import sys,os, time

#Define absolute/relative paths to scripts and tools
SCRIPTPATH=str(sys.path[0])+"\/"
AMISCRIPTS=SCRIPTPATH+"../ami-scripts/"
CDRSCRIPTS=SCRIPTPATH+"../cdr-scripts/"
TELNET="/usr/bin/telnet"
GMETRIC="/usr/local/bin/gmetric"

#
HOSTNAME="PBX1"


##########################
#Service Summary Stats
##########################

#Active Calls in PBX
#os.system(AMISCRIPTS + "active_calls.sh")
os.system(GMETRIC+" --name " + HOSTNAME + "-No_Current_Calls --value `" + AMISCRIPTS + "active_calls.sh` --type int8 --units Calls")

#Average Duration of calls
#os.system(CDRSCRIPTS + "call_duration.py")
os.system(GMETRIC+" --name " + HOSTNAME + "-Avg_Call_Duration --value `" + CDRSCRIPTS + "call_duration.py` --type int8 --units Seconds")

#Number of Mobile Calls
#os.system(CDRSCRIPTS + "no_mobile_calls.py")
os.system(GMETRIC+" --name " + HOSTNAME + "-No_Mobile_Calls --value `" + CDRSCRIPTS + "no_mobile_calls.py` --type int8 --units Calls")


#Number of Local Calls
#os.system(CDRSCRIPTS + "no_local_calls.py")
os.system(GMETRIC+" --name " + HOSTNAME + "-No_Local_Calls --value `" + CDRSCRIPTS + "no_local_calls.py` --type int8 --units Calls")

#Number of Internal Calls
#os.system(CDRSCRIPTS + "no_local_calls.py")
os.system(GMETRIC+" --name " + HOSTNAME + "-No_Internal_Calls --value `" + CDRSCRIPTS + "no_internal_calls.py` --type int8 --units Calls")

#Number of International Calls
#os.system(CDRSCRIPTS + "no_international_calls.py")
os.system(GMETRIC+" --name " + HOSTNAME + "-No_International_Calls --value `" + CDRSCRIPTS + "no_international_calls.py` --type int8 --units Calls")

#Number of Interstate Calls
#os.system(CDRSCRIPTS + "no_interstarte_calls.py")
os.system(GMETRIC+" --name " + HOSTNAME + "-No_Interstate_Calls --value `" + CDRSCRIPTS + "no_interstate_calls.py` --type int8 --units Calls")


##########################
#Telephony Summary Stats
##########################

# Some of metrics are already injected in the Service Summary section

#Uptime of Asterisk
#os.system(AMISCRIPTS + "active_channels.sh")
os.system(GMETRIC+" --name " + HOSTNAME + "-Asterisk_Uptime --value \"`" + AMISCRIPTS + "asterisk-uptime.py`\" --type string")

#Last Asterisk Config Reload
#os.system(AMISCRIPTS + "active_channels.sh")
#os.system(GMETRIC+" --name " + HOSTNAME + "-AsteriskReloadtime --value \"`" + AMISCRIPTS + "asterisk-reloadtime.py`\" --type string")


#Active Channels in PBX
#os.system(AMISCRIPTS + "active_channels.sh")
os.system(GMETRIC+" --name " + HOSTNAME + "-No_Current_Channels --value `" + AMISCRIPTS + "active_channels.sh` --type int8 --units Channels")







##########################
# Client Summary Stats
##########################

# These stats will show if client are in a call or not
# 


os.system(AMISCRIPTS+"asterisk-clients.py")
